package com.prp.service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;


import com.prp.dao.ItemDAO;
import com.prp.dao.ShowCode;
import com.prp.dao.ShowCodeDAO;
import com.prp.interfaces.ISearchServ;
import com.prp.struts.bean.PageValue;

public class SearchServImpl implements ISearchServ{

	public List getFileListByPage(Map searchInfo, PageValue size) {
		// TODO Auto-generated method stub
		
		ItemDAO dao = new ItemDAO();
		String queryString = " from Item as m where 'a' = 'a' ";
		String fileName = (String) searchInfo.get("fileName");
		if (fileName != null){
			String type = (String)searchInfo.get("fileNameType");
			if (type.equals("exact")){
				queryString +=" and (m.fileName =:fileName "
							+" or m.fileNameCh =:fileName)";
			}
			else{
				queryString +=" and  (m.fileName like '%'||:fileName||'%' "
							+" or  m.fileNameCh like '%'||:fileName||'%' )";
			}
		}
		String publishO = (String) searchInfo.get("publishO");
		if (publishO != null){
			String type = (String)searchInfo.get("publishOType");
			if (type.equals("exact")){
				queryString +="  and (m.publishOrgan =:publishO "
							+" or m.publishOrganCh =:publishO)";
			}
			else{
				queryString +=" and  (m.publishOrgan like '%'||:publishO||'%' "
							+ " or  m.publishOrganCh like '%'||:publishO||'%') ";
			}
		}
		
		String mainCont = (String) searchInfo.get("mainCont");
		if (mainCont != null){
			String type = (String)searchInfo.get("mainContType");
			if (type.equals("exact")){
				queryString +=" and  (m.mainContent =:mainCont "
							+" or m.mainContentCh =:mainCont)";
			}
			else{
				queryString +=" and  (m.mainContent like '%'||:mainCont||'%' "
							+" or  m.mainContentCh like '%'||:mainCont||'%' )";
			}
		}
		String dataType = (String) searchInfo.get("dataType");
		if (dataType != null){
			queryString +=" and  m.type =:dataType ";
		}
		String country = (String) searchInfo.get("country");
		if (country != null){
			queryString +=" and  m.country =:country ";
		}
		String language = (String) searchInfo.get("language");
		if (language != null){
			queryString +=" and  m.language =:language ";
		}
		Integer fileStat = (Integer) searchInfo.get("fileStat");
		if (fileStat != null){
			queryString +=" and  m.state =:fileStat ";
		}
		String yeatSter = (String)searchInfo.get("year");
		Date startDate = null;
		if (yeatSter != null){
			Integer year = Integer.parseInt(yeatSter);
			SimpleDateFormat df2 = new SimpleDateFormat("yyyy");
			startDate = new Date();
			
			Calendar cal=Calendar.getInstance();
			//cal.set(Calendar.YEAR, Integer.parseInt(df2.format(startDate)));

			cal.set(Integer.parseInt(df2.format(startDate)), 0, 1);

			cal.add(Calendar.YEAR, -year);
			startDate = cal.getTime();

			queryString +=" and  m.publishTime >:startDate ";
			
		}		
		
		Query queryObject = dao.getSession().createQuery(queryString);
		/*
		queryObject.setFirstResult(pageNum*pageSize - pageSize);
		queryObject.setMaxResults(pageSize);
		*/
		if (fileName != null)
			queryObject.setParameter("fileName", fileName);
		if (publishO != null)
			queryObject.setParameter("publishO", publishO);
		if (mainCont != null)
			queryObject.setParameter("mainCont", mainCont);
		if (dataType != null)
			queryObject.setParameter("dataType", dataType);
		if (country != null)
			queryObject.setParameter("country", country);
		if (language != null)
			queryObject.setParameter("language", language);
		if (startDate != null)
			queryObject.setParameter("startDate", startDate);
		if (fileStat != null)
			queryObject.setParameter("fileStat", fileStat);
		
		size.setPageCount(queryObject.list().size());
		int pageCount = size.getPageCount();
		int pageSize = size.getPageSize();
		int pageNow = size.getPageNow();
		if(pageNow <= 0)
			pageNow = 1;
		
		if(pageCount%pageSize == 0){
			if(pageNow > pageCount/pageSize)
				pageNow = pageCount / pageSize;
		}else{
			if(pageNow > pageCount/pageSize + 1)
				pageNow = pageCount / pageSize + 1;
		}
		size.setPageNow(pageNow);
		size.setPageSize(pageSize);
		queryObject.setFirstResult(pageNow*pageSize - pageSize);
		queryObject.setMaxResults(pageSize);
		
		
		

		return queryObject.list();
	}
	
	public List easySearchByPage(String info, PageValue size, Integer stat){
		ItemDAO dao = new ItemDAO();
		ShowCodeDAO d2 = new ShowCodeDAO();
		List temp = d2.findByShowValue(info);
		String str= null;
		if (temp.size() != 0)
			str = ((ShowCode)temp.get(0)).getCode();
		
		
		return dao.searchFileByPage(info, str, stat, size);
	}

}
